package com.pokermind.dse.tcp.client.interactive;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import java.util.Map;

/**
 * JSON格式化工具
 * 
 * @author PokerMind Interactive Client
 */
public class JsonFormatter {
    
    private static final ObjectMapper mapper = new ObjectMapper();
    
    static {
        mapper.enable(SerializationFeature.INDENT_OUTPUT);
    }
    
    /**
     * 格式化Map为JSON字符串
     */
    public static String format(Map<String, Object> map) {
        if (map == null || map.isEmpty()) {
            return "{}";
        }
        
        try {
            return mapper.writeValueAsString(map);
        } catch (Exception e) {
            return map.toString();
        }
    }
    
    /**
     * 格式化Map为紧凑的单行JSON
     */
    public static String formatCompact(Map<String, Object> map) {
        if (map == null || map.isEmpty()) {
            return "{}";
        }
        
        try {
            ObjectMapper compactMapper = new ObjectMapper();
            return compactMapper.writeValueAsString(map);
        } catch (Exception e) {
            return map.toString();
        }
    }
    
    /**
     * 打印格式化的请求（紧凑单行，V4.25简化）
     */
    public static void printRequest(String type, Map<String, Object> request) {
        System.out.println(type + "-请求: " + formatCompact(request));
    }
    
    /**
     * 打印格式化的响应（紧凑单行，V4.25简化）
     */
    public static void printResponse(String type, Map<String, Object> response) {
        System.out.println(type + "-响应: " + formatCompact(response));
    }
}

